package com.wjs.sync;
class Ticket {
    private int num=30;
    public synchronized void sale() throws InterruptedException {
        if(num>0){
            Thread.sleep(300);
            System.out.println(Thread.currentThread().getName()+":卖出:"+(num--)+"剩余:"+num);
        }
    }
}
public class SaleTicket {
    private static volatile int num=30;
    public static void main(String[] args)  throws InterruptedException {
        Ticket ticket = new Ticket();
        new Thread(()->{
            for(int i=0;i<40;i++){
                try {
                    ticket.sale();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"aa").start();
        new Thread(()->{
            for(int i=0;i<40;i++){
                try {
                    ticket.sale();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"bb").start();
        new Thread(()->{
            for(int i=0;i<40;i++){
                try {
                    ticket.sale();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"cc").start();
    }
}
